package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.ResponseVO;
import com.example.mapper.FamilyMapper;
import com.example.pojo.FamilyInfo;
import com.example.pojo.dto.UserParentInfoDTO;
import com.example.service.FamilyInfoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class FamilyInfoServiceImpl implements FamilyInfoService {
    @Resource
    private FamilyMapper familyMapper;

    @Override
    public String addOrUpdateFamilyInfo(FamilyInfo familyInfo) {
        String result = "";
        if(Objects.isNull(familyInfo.getMemberId())) {
            familyMapper.insert(familyInfo);
            result="添加成功";
        }else{
            familyMapper.updateById(familyInfo);
            result="修改成功";
        }
        return result;
    }

    @Override
    public ResponseEntity<ResponseVO<List<FamilyInfo>>> selectFamilyByStuId(Long childId) {
        QueryWrapper<FamilyInfo> familyInfoQueryWrapper = new QueryWrapper<>();
        familyInfoQueryWrapper.eq("child_id",childId);
        List<FamilyInfo> familyInfos = familyMapper.selectList(familyInfoQueryWrapper);
        if (!familyInfos.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "家庭信息获取成功", familyInfos));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "暂无数据"));
    }

    @Override
    public UserParentInfoDTO selectFamilyByStuName(String stuName) {
        return familyMapper.selectFamilyByStuName(stuName);
    }

}
